package think.net;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * socket服务端
 * 
 * @author tanqq
 *
 */
public class Server {
	public static void init() throws IOException {
		// 创建socket服务
		@SuppressWarnings("resource")
		final ServerSocket server = new ServerSocket(8989);
		while (true) {
			Socket soc = server.accept();
			System.out.println(soc.getInetAddress() +":"+soc.getPort()+ " 已经连接...");
			new Thread(()->{
				try (
						// 获取套接字对象
						BufferedReader in = new BufferedReader(new InputStreamReader(soc.getInputStream()));
						PrintWriter pw = new PrintWriter(soc.getOutputStream());
					) {
					// 获取输入输出流
					String s = in.readLine();
					System.out.println("->" + s);
					pw.println(s.toString().toUpperCase());
					pw.flush();

				}catch (Exception e) {
					e.printStackTrace();
				}
			}).start();
		}
		

	}

	public static void main(String[] args) throws IOException {
		Server.init();

	}
}
